/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using System;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif
namespace TFrameWork.Module.ActionEditor
{
    [System.Serializable]
    public class TrackItemData : DataBase
    {
        public bool isReguiar;
        public bool isLock;

        /// <summary>
        /// 是否禁用
        /// </summary>
        public bool IsDisable = false;

        /// <summary>
        /// 轨道名称
        /// </summary>
        public string trackName;

        /// <summary>
        /// 轨道事件
        /// </summary>
        public List<TrackEventBase> events = new List<TrackEventBase>();


        #region Method

        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="eve"></param>
        public void AddEvent(TrackEventBase eve)
        {
            if (!events.Contains(eve))
            {
                eve.OnChangeIndex = Sort;
                events.Add(eve);
                Sort();
            }
            else
            {
                ActionEditorLog.Error("当前事件已经被添加 " + eve.trackName);
            }
        }

        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="eve"></param>
        public void RemoveEvent(TrackEventBase eve)
        {
            if (events.Contains(eve))
            {
                events.Remove(eve);
                Sort();
            }
            else
            {
                ActionEditorLog.Error("当前事件不存在");
            }
        }

        public List<TrackEventBase> TryGetTrack(int startIndex)
        {
            List<TrackEventBase> list = new List<TrackEventBase>();
            for (int i = 0; i < events.Count; i++)
            {
                if (startIndex == events[i].startIndex)
                {
                    list.Add(events[i]);
                }
                else if (events[i].startIndex > startIndex)
                {
                    break;
                }
            }

            return list;
        }

        public List<TrackEventBase> TryGetTickEvent(int currentIndex)
        {
            List<TrackEventBase> list = new List<TrackEventBase>();
            for (int i = 0; i < events.Count; i++)
            {
                var eventData = events[i];
                if (currentIndex >= eventData.startIndex &&
                    currentIndex <= eventData.startIndex + eventData.durationIndex)
                {
                    list.Add(events[i]);
                }
                else if (currentIndex < eventData.startIndex)
                {
                    break;
                }
            }

            return list;
        }

        /// <summary>
        /// 按照事件起始帧进行排序 todo 待测试
        /// </summary>
        public void Sort()
        {
            events.Sort((p, n) =>
            {
                if (p.startIndex > n.startIndex)
                {
                    return 1;
                }
                else if (p.startIndex < n.startIndex)
                {
                    return -1;
                }

                return 0;
            });
        }


        public override object Clone()
        {
            TrackItemData clone = base.Clone() as TrackItemData;
            clone.isReguiar = isReguiar;
            clone.isLock = isLock;
            clone.IsDisable = IsDisable;
            clone.trackName = trackName;
            clone.events = new List<TrackEventBase>();
            for (int i = 0; i < events.Count; i++) 
            {
                clone.events.Add(events[i].Clone() as TrackEventBase);
            }

            return clone;
        }

        #endregion
    }
}
